package com.da.javatest.jvm;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodHandles.Lookup;
import java.lang.invoke.MethodType;
import java.lang.reflect.Method;

class FooTest1 {
    private static void bar(Object o) {
        System.out.println(123 + o.toString());
    }

    public static Lookup lookup() {
        return MethodHandles.lookup();
    }

    public static void main(String[] args) throws Throwable {
        // 获取方法句柄的不同方式
        MethodHandles.Lookup l = FooTest1.lookup(); // 具备 Foo 类的访问权限
        Method m = FooTest1.class.getDeclaredMethod("bar", Object.class);
        MethodHandle mh0 = l.unreflect(m);
        mh0.invoke("aaa");

        MethodType t = MethodType.methodType(void.class, Object.class);
        MethodHandle mh1 = l.findStatic(FooTest1.class, "bar", t);
        mh1.invoke("bbb");

    }
}